package com.fzy.connect;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

import com.fzy.Source.DB;
import com.fzy.Source.Source;

public class UDPSendNoIP extends Thread {
	DatagramSocket ds = null;
	DatagramPacket sendDp;
	InetAddress address;
	private int id;

	public UDPSendNoIP(int id) {
		this.id = id;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		List<String> messages = DB.getAllMessages(id);
		try {
			address = InetAddress.getByName(Source.getLoginIp(id));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (String string : messages) {
			byte[] message = string.getBytes();
			sendDp = new DatagramPacket(message, message.length, address,
					Source.CLIENT_GETPORT);
			try {
				ds = new DatagramSocket();
				ds.send(sendDp);
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}